public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    private TreeNode root;

    // 查找
    // 1. 树为空树，返回false
    // 2. 树不为空，按照搜索树的性质查找
    // 查找成功返回true，查找失败返回false
    // 注意：
    // 1. 查找成功，说明树中存在该值，返回true
    // 2. 查找失败，说明树中不存在该值，返回false
    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (key < cur.val) {
                cur = cur.left;
            } else if (key > cur.val) {
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }


    // 插入
    // 1. 树为空树，直接插入
    // 2. 树不为空，按照搜索树的性质查找插入位置，插入新节点
    // 插入成功返回true，插入失败返回false
    // 注意：
    // 1. 新插入的节点一定是叶子节点
    // 2. 插入失败，说明树中已经存在该值，返回false
    // 3. 插入成功，说明树中不存在该值，返回true
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null) {
            if (cur.val > key) {
                prev = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                prev = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }
        if (prev.val > key) {
            prev.left = new TreeNode(key);
        }
        if (prev.val < key) {
            prev.right = new TreeNode(key);
        }
        return true;
    }

    public void remove(int val) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur!= null) {
            if (cur.val > val) {
                prev = cur;
                cur = cur.left;
            }   else if (cur.val < val) {
                prev = cur;
                cur = cur.right;
            }   else {
                removeNode(prev,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode prev, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = root.right;
            } else if (cur == prev.left) {
                prev.left = cur.right;
            } else {
                prev.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = root.left;
            } else if (cur == prev.left) {
                prev.left = cur.left;
            } else {
                prev.right = cur.left;
            }
        } else {
            TreeNode targetparent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetparent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (target == targetparent.left) {
                targetparent.left = target.right;
            } else {
                targetparent.right = target.right;
            }
        }
    }
}
